﻿using System;
using System.Data;
using TrafficArchives.Client;
using TrafficArchives.DataAccess;
using TrafficArchives.Utilities;

namespace TrafficArchives.SafeEducate
{
    public partial class SafeEducateAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initilize();
            }
        }

        private void Initilize()
        {
            var action = Request.QueryString["action"];
            var id = Request.QueryString["id"];
            LblTitle.Text = "添加安全教育";
            TableQuery.Visible = string.IsNullOrEmpty(action); // 只有新增才显示。
            bool isDisplayData = false;

            var reasonData = new ValueObjectDb().GetDataByClassCode(AppSettings.培训原因编码);
            DdlTrainReason.DataSource = reasonData;
            DdlTrainReason.DataTextField = "Name";
            DdlTrainReason.DataValueField = "Code";
            DdlTrainReason.DataBind();

            if (string.IsNullOrEmpty(action))
            {
                LblManageOrgan.Text = LoginUser.Current.DepartmentName;
                LblManageOrganID.Value = LoginUser.Current.DepartmentId;
                DivSave.Visible = DivClear.Visible = true;
                DivBack.Visible = false;
            }
            else if (action == "look")
            {
                LblTitle.Text = "查看安全教育";
                DivSave.Visible = DivClear.Visible = false;
                DivBack.Visible = true;
                TxtContent.Enabled = false;
                TxtTrainAddr.Enabled = false;
                TxtTecher.Enabled = false;
                DdlTrainReason.Enabled = false;
                isDisplayData = true;
            }
            else if (action == "edit")
            {
                LblTitle.Text = "修改安全教育";
                DivSave.Visible = true;
                DivBack.Visible = true;
                DivClear.Visible = false;
                isDisplayData = true;
            }
            if (isDisplayData)
            {
                var db = new SafeEducateDb();
                var data = db.GetData(id);
                DisplayArchives(data);
            }
        }

        private void DisplayArchives(DataTable data)
        {
            if (data.Rows.Count > 0)
            {
                var dr = data.Rows[0];
                LblName.Text = dr["NAME"].ToString();
                //LblIdentityNo.Text = dr["ZJCODE"].ToString();
                //LblArchiveNo.Text = dr["DACODE"].ToString();
                LblTrainDate.Text = dr.IsNull("PXDATE") ? string.Empty : DateTime.Parse(dr["PXDATE"].ToString()).ToString("yyyy-MM-dd");
                
                TxtTrainAddr.Text = dr["PXADDRESS"].ToString();
                TxtTecher.Text = dr["JYPERSON"].ToString();
                TxtContent.Text = dr["PXCONTENT"].ToString();

                if (DdlTrainReason.Items.FindByValue(dr["PXYYID"].ToString()) != null)
                {
                    DdlTrainReason.SelectedValue = dr["PXYYID"].ToString();
                }
            }
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            var client = new ServiceClient();
            var data = client.GetDrivingLicence(TxtIdentityNo.Text, TxtArchivesNo.Text);
            TxtWebServiceMsg.Visible = data.WebServiceIsError;
            UpdatePanelCondition.Update();
            if (!data.WebServiceIsError && data.Rows.Count > 0)
            {
                var dataRow = data.Rows[0];
                LblName.Text = dataRow["xm"].ToString();
                LblSex.Text = dataRow["xb"].ToString();
                LblIdentityType.Text = dataRow["sfzmmc"].ToString();
                LblIdentityNo.Text = dataRow["sfzmhm"].ToString();
                LblBorthDay.Text = dataRow["csrq"].ToString();
                LblArchiveNo.Text = dataRow["dabh"].ToString();
                LblFirstLicensing.Text = dataRow["cclzrq"].ToString();
                LblCarType.Text = dataRow["zjcx"].ToString();
                LblDriverCardStatus.Text = dataRow["zt"].ToString();
                LblAuditDate.Text = dataRow["yxqz"].ToString(); //审验日期
                //LblManageOrgan.Text = dataRow[""].ToString(); // 管理部门
                LblLicensingOrgan.Text = dataRow["fzjg"].ToString(); // 发证机关
                LblAdminDivider.Text = dataRow["xzqh"].ToString();
                BtnSave.Enabled = true;
            }
            else
            {
                LblName.Text = string.Empty;
                LblSex.Text = string.Empty;
                LblIdentityType.Text = string.Empty;
                LblIdentityNo.Text = string.Empty;
                LblBorthDay.Text = string.Empty;
                LblArchiveNo.Text = string.Empty;
                LblFirstLicensing.Text = string.Empty;
                LblCarType.Text = string.Empty;
                LblDriverCardStatus.Text = string.Empty;
                LblAuditDate.Text = string.Empty;
                LblManageOrgan.Text = string.Empty;
                LblLicensingOrgan.Text = string.Empty;
                LblAdminDivider.Text = string.Empty;
                BtnSave.Enabled = false;
            }
            UpdatePanelForm.Update();
        }

        protected void BtnSaveClicked(object sender, EventArgs e)
        {
            var id = Request.QueryString["id"];
            var db = new SafeEducateDb();
            var data = string.IsNullOrEmpty(id) ? db.GetNoneData() : db.GetData(id);
            if (string.IsNullOrEmpty(id))
            {
                var newRow = data.NewRow();
                newRow["AQJYTABLE_ID"] = Guid.NewGuid().ToString();
                data.Rows.Add(newRow);
            }
            if (data.Rows.Count > 0)
            {
                var dr = data.Rows[0];
                dr["NAME"] = LblName.Text;
                if (!string.IsNullOrEmpty(LblTrainDate.Text))
                {
                    DateTime trainDate;
                    if (DateTime.TryParse(LblTrainDate.Text, out trainDate))
                    {
                        dr["PXDATE"] = trainDate;
                    }
                }
                dr["PXADDRESS"] = TxtTrainAddr.Text;
                dr["JYPERSON"] = TxtTecher.Text;
                dr["PXYYID"] = DdlTrainReason.SelectedValue;
                dr["PXYYNAME"] = DdlTrainReason.SelectedItem.Text;
                dr["PXCONTENT"] = TxtContent.Text;
            }
            if (string.IsNullOrEmpty(id))
            {
                db.Add(data);
                //var log = new LogInfo(LogType.);
                //LogWriter.Writer(log);
            }
            else
            {
                db.Update(data);
                //var log = new LogInfo(LogType.修改安全教育);
                //LogWriter.Writer(log);
            }

            DivSucess.Visible = true;
            UpdatePanelForm.Update();
        }
    }
}